using UnityEngine;
using UnityEditor;
using UnityEngine.UI;
using ZSW.Framework.Utils;
using System.Collections.Generic;

namespace ZSW.Framework.EditorTools
{
    public class DestroyEmptyObject
    {
        [MenuItem("Tools/GameObject/Destory Empty Object")]
        public static void Invoke()
        {
            for (int i = 0; i < 10; i++)
            {
                var allObjects = Resources.FindObjectsOfTypeAll<GameObject>();
                List<GameObject> emptyObjects = new List<GameObject>();

                foreach (var obj in allObjects)
                {
                    // 当前物体下不存在子物体，且当前物体只有Transform组件
                    if (obj.transform.childCount == 0 && obj.GetComponents<Component>().Length < 2)
                    {
                        emptyObjects.Add(obj);
                    }
                }
                foreach (var emptyObject in emptyObjects)
                {
                    Undo.DestroyObjectImmediate(emptyObject);
                }
                ZSWFUtil_Debug.Log($"第{i}/10轮 清除完成 共清除{emptyObjects.Count}个空物体");
            }

        }
    }
}

